$(document).ready(function() {
	$("#username").bind("keyup", function(){
		existUsername(this);
    });
	 
    $("#password").bind("keyup", function(){
    	checkSecurityPassword(this);
    });
    
    $("#confirmPassword").bind("keyup", function(){
    	checkConfirmedPassword(this, $("#password"));
    });
    
    $("#email").bind("keyup", function(){
    	checkEmail(this);
    });
    
    $("#confirmEmail").bind("keyup", function(){
    	checkConfirmedEmail(this, $("#email"));
    });
    
    $("#dataNascita").datepicker({
        showOn: "both",
        buttonImage: "pecuswap/assets/img/calendar.png",
        buttonImageOnly: true,
        changeYear: true,
        buttonText: "Seleziona una data",
        dateFormat: "dd/mm/yy",
        yearRange: "c-80:c"
      });
});

function existUsername(usernameInput){
	$.post($("#existUsernameAction").val(), $("#registrazioneForm").serialize(), function(data) {
		$("#divUsername").show();
		$("#divUsername").html(data);
	});
}

function checkSecurityPassword(passwordInput){
	$.post($("#checkPasswordSecurityAction").val(), $("#registrazioneForm").serialize(), function(data) {
		$("#divSecurityPassword").show();
		$("#divSecurityPassword").html(data);
	});
}

function checkConfirmedPassword(passwordInput){
	$.post($("#checkConfirmedPasswordAction").val(), $("#registrazioneForm").serialize(), function(data) {
		$("#divConfirmPassword").show();
		$("#divConfirmPassword").html(data);
	});
}

function checkEmail(passwordInput){
	$.post($("#checkEmailAction").val(), $("#registrazioneForm").serialize(), function(data) {
		$("#divEmail").show();
		$("#divEmail").html(data);
	});
}

function checkConfirmedEmail(emailInput){
	$.post($("#checkConfirmedEmailAction").val(), $("#registrazioneForm").serialize(), function(data) {
		$("#divUsername").show();
		$("#divConfirmEmail").html(data);
	});
}